package com.qa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qa.common.ServerResponse;
import com.qa.dao.CategoryArticleMapper;
import com.qa.dao.CategoryMapper;
import com.qa.pojo.Category;
import com.qa.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author
 * @Date 2020/3/3 21:18
 * @Desc
 **/
@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CategoryArticleMapper categoryArticleMapper;

    @Override
    public PageInfo<Category> getAll(String categoryName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Category> categoryList = categoryMapper.getByCategoryName(categoryName);
        return new PageInfo<>(categoryList);
    }

    @Override
    public Category getById(Integer categoryId) {
        return categoryMapper.selectByPrimaryKey(categoryId);
    }

    @Override
    public List<Category> getByStatus() {
        return categoryMapper.getByStatus();
    }

    @Override
    public ServerResponse save(Category category) {
        int result=0;
        if (category.getId() == null) {
            category.setStatus("1");
            result = categoryMapper.insert(category);
        } else {
            categoryArticleMapper.updateByCategoryId(category.getId(), category.getCategoryName());
            result = categoryMapper.updateByPrimaryKey(category);

        }
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("成功！");
        }
        return ServerResponse.createByErrorMessage("失败！");
    }

    @Override
    public ServerResponse deleteById(Integer categoryId) {
        int result = categoryMapper.deleteByPrimaryKey(categoryId);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("成功！");
        }
        return ServerResponse.createByErrorMessage("失败！");
    }


}
